package com.annhoa.core.web;

import java.sql.Timestamp;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.annhoa.core.base.BasePage;
import com.annhoa.util.PageData;

public class BaseController {

    // private static Logger logger =
    // Logger.getLogger(baseController.class.toString());
    protected BasePage webpage;

    /**
     * 得到PageData
     */
    public PageData getPageData() {
        return new PageData(this.getRequest());
    }

    /**
     * 得到ModelAndView
     */
    public ModelAndView getModelAndView() {
        return new ModelAndView();
    }

    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return request;
    }

    /**
     * 得到response对象
     */
    public HttpServletResponse getResponse() {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getResponse();
        response.setHeader("Access-Control-Allow-Origin", "*");
        return response;
    }

    /**
     * 得到Timestamp(String)
     */
    public String getTimestamp() {
        return new Timestamp(System.currentTimeMillis()).toString();
    }

    /**
     * 得到分页列表的信息
     */
    public BasePage getBasePage() {

        return new BasePage();
    }

    public static void logBefore(Logger logger, String interfaceName) {
        logger.info("");
        logger.info("start");
        logger.info(interfaceName);
    }

    public static void logAfter(Logger logger) {
        logger.info("end");
        logger.info("");
    }

}
